package com.lemon.components.ant;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import com.alibaba.fastjson.JSONArray;
import com.lemon.components.ant.beans.TableColumns;
import com.lemon.components.ant.beans.TemplateContext;
import com.lemon.components.ant.util.ClassUtil;
import com.lemon.components.ant.util.CodeMakerPropertiesUtil;
import com.lemon.components.ant.util.TemplateUtil;

public class HtmlTemplateAntTask extends Task {

	private String projectdir;
	private String serviceName;
	private String controllerPrefix;
	private String modelName;
	private static Properties p = new Properties();
	static{
		try {
			p.load(HtmlTemplateAntTask.class.getResourceAsStream("/code/codemaker.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void execute() throws BuildException {
		String templatepath = projectdir+"\\src\\main\\resources\\code\\codeTemplate";
		String outputfilepath = projectdir+CodeMakerPropertiesUtil.get("html.path")+"/"+serviceName;
		String javaScriptpath = projectdir+CodeMakerPropertiesUtil.get("js.path")+CodeMakerPropertiesUtil.get("js.prefix")+"/"+serviceName;
		
		TemplateContext context = new TemplateContext();
		HashMap<String, Object> params = new HashMap<>();
		List<TableColumns> columnList = JSONArray.parseArray(CodeMakerPropertiesUtil.get("columns"), TableColumns.class);
		ClassUtil.setType(columnList, p.getProperty("modelName"));
		List<TableColumns> windowItem = new ArrayList<TableColumns>();
		for(TableColumns tmp:columnList){
			if(!tmp.getCode().equalsIgnoreCase("rowUpdateTime")
					&&!tmp.getCode().equalsIgnoreCase(p.getProperty("primaryKey"))
					&&!tmp.getCode().equalsIgnoreCase("rowAddTime")
					&&!tmp.getCode().equalsIgnoreCase("rawAddTime"))
				windowItem.add(tmp);
		}
//		log("列信息：\n"+JSONArray.toJSONString(columnList, SerializerFeature.PrettyFormat),Project.MSG_WARN);
		context.setServiceName(serviceName);
		context.setJsprefix(CodeMakerPropertiesUtil.get("js.prefix"));
		String[] jspathtmp = CodeMakerPropertiesUtil.get("js.path").split("/");
		context.setJspath("/"+jspathtmp[jspathtmp.length-1]);
		context.setModelName(modelName);
		context.setControllerPrefix(controllerPrefix);
		context.setColumns(columnList);
		context.setPrimaryKey(p.getProperty("primaryKey"));
		params.put("info", context);
		params.put("windowItem", windowItem);
//		log(params.toString());
		//HTML
		TemplateUtil.getInstance(templatepath).build(
				"html/TemplateTable.html",
				outputfilepath+"\\"+serviceName+"-table-page.html",
				params
			);
		//JS
		TemplateUtil.getInstance(templatepath).build(
				"html/TemplateJS.js",
				javaScriptpath+"\\"+serviceName+"-table.js",
				params
			);
	}
	
	public String getServiceName() {
		return serviceName;
	}
	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}
	public String getControllerPrefix() {
		return controllerPrefix;
	}
	public void setControllerPrefix(String controllerPrefix) {
		this.controllerPrefix = controllerPrefix;
	}

	public String getProjectdir() {
		return projectdir;
	}

	public void setProjectdir(String projectdir) {
		this.projectdir = projectdir;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}
}
